<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{item.name}}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="saveAssignRoles">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import {getRoles} from '@/api/setting'
import {getUserDetailById} from '@/api/user'
import {assignRoles} from '@/api/employees'
export default {
  props:{
    id:{
      type:String,
      required:true
    }
  },
  data() {
    return {
      list:[],//保存角色列表
      roleIds: []//el-checkbox组件label后面的值，如果当前复活矿选中就把label后面的值保存 到这里
    }
  },
  created(){
    this.loadRoles()
  },
  methods: {
    async saveAssignRoles(){
      try {
        const res= await assignRoles({id:this.id,roleIds:this.roleIds})
        this.$emit('success')
      } catch (error) {
        console.log('assignRoles',error);
      }
    },
    async loadRoles(){
      try {
        const res= await getRoles({page:1,pagesize:100})
        this.list=res.data.rows
        const info=await getUserDetailById(this.id)
        this.roleIds=info.data.roleIds
      } catch (error) {
        console.log(error);
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>